package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper1 setmealMapper1;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    @Override
    public Result add(SetmealDTO setmealDTO) {
        //非空判断
        if(setmealDTO.getName()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //映射
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入数据
        setmealMapper1.add(setmeal);
        //获取生成的套餐id
        Long id = setmeal.getId();
        //保存套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(id);});
        //保存套餐和菜品的关联关系
        setmealDishMapper.add(setmealDishes);
        //返回
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result page(SetmealPageQueryDTO dto) {
        //非空判断
        if(dto.getPage()==null){
            dto.setPage(1);
        }
        if(dto.getPageSize()==null){
            dto.setPageSize(5);
        }
        //分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //条件查询
        Page<Setmeal> setmeals = setmealMapper1.pageSetmeal(dto);
        //封装
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmeals.getTotal());
        pageResult.setRecords(setmeals.getResult());
        //返回
        return Result.success(pageResult);
    }

    /**
     * 修改启用状态
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result updateStatus(Integer status, Long id) {
        //非空判断
        if(status==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if(id==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //调用mapper修改状态
        Setmeal setmeal =Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper1.update(setmeal);
        //返回
        return Result.success();
    }

    /**
     * 根据id查询回显
     *
     * @param id
     * @return
     */
    @Override
    //套餐id
    public Result getById(Long id) {
        //非空判断
        if(id==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //调用setmealMapper根据套餐id查询套餐
        Setmeal setmeal = setmealMapper1.getByIdWithDish(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //补充VO分类名称还有菜品表
        Long categoryId = setmeal.getCategoryId();//分类表的id
        Category category = categoryMapper.getById(categoryId);
        setmealVO.setCategoryName(category.getName());
        //获取套餐信息
        List<SetmealDish> setmealDishList = setmealDishMapper.getSetmealId(id);
        setmealVO.setSetmealDishes(setmealDishList);
        return Result.success(setmealVO);
    }

    /**
     * 修改
     *
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public Result update(SetmealDTO setmealDTO) {
        //非空判断
        if(setmealDTO.getId()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //调用mapper接口
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper1.update(setmeal);
        //2、删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        Long dtoId = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(dtoId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(dtoId);
        });
        //3、重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行add
        setmealDishMapper.add(setmealDishes);

        //返回值
        return Result.success();
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result delete(List<Long> ids) {
        //非空判断
        if(ids==null || ids.size()==0){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper1.getByIdWithDish(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(setmealId -> {
            //删除套餐表中的数据
            setmealMapper1.deleteById(setmealId);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealId(setmealId);
        });
        //返回数据
        return Result.success();
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper1.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper1.getDishItemBySetmealId(id);
    }
}
